package com.wkbb.sports.controller;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.sports.dto.JumpClassSummaryDto;
import com.wkbb.sports.dto.JumpGradeSummaryDto;
import com.wkbb.sports.dto.JumpTrainSummaryDto;
import com.wkbb.sports.dto.StatisticsAvgScoreDto;
import com.wkbb.sports.dto.TrainStudentSummaryDto;
import com.wkbb.sports.dto.TrainSummaryDto;
import com.wkbb.sports.enums.SportsModelTypeEnum;
import com.wkbb.sports.enums.SportsProjectKindEnum;
import com.wkbb.sports.service.StatisticsService;
import com.wkbb.sports.service.StatisticsService.Level;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Api(description = "统计管理")
@RequestMapping("/sports/{apiVersion}/statistics")
@Slf4j
public class StatisticsController extends BaseController {

	
	@Autowired
	private StatisticsService statisticsService;
	
	@ApiOperation(value = "获取统计信息")
	@RequestMapping(value = "/queryTrainSummaryList", method = RequestMethod.GET)
	public ResultDTO<?> queryTrainSummaryList(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "项目类型", required = true) @RequestParam(name = "projectKind", required = true) Byte projectKind,
			@ApiParam(value = "模式", required = true) @RequestParam(name = "modelType", required = true) Byte modelType,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId){
		log.info("queryTrainSummaryList(projectKind:" + projectKind + ", modelType:" + modelType +
				", orgId:" + orgId + ", gradeId:" + gradeId + ")");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		if(month < 8) {
			year--;
		}
		cal.set(year, 8, 1);
		Date beginTime = cal.getTime();
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", df.format(beginTime));
		params.put("endTime", df.format(now));
		params.put("projectKind", projectKind);
		params.put("modelType", modelType);
		params.put("orgId", orgId);
		params.put("gradeId", gradeId);
		log.info("beginTime:" + beginTime + ", endTime:" + now);
		String trianIds;
		try{
			trianIds = statisticsService.getTrainIds(params);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}
		List<TrainSummaryDto> array = null;
		if(trianIds != null) {
			try{
				array = statisticsService.queryTrainSummaryList(trianIds, gradeId);
			}catch(Throwable e) {
				logError(e);
				return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
			}
		}
		TrainStudentSummaryDto studentSummary = null;
		if(trianIds != null) {
			try {
				studentSummary = statisticsService.getTrainStudentSummary(trianIds);
			}catch(Throwable e) {
				logError(e);
				return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
			}
		}
		
		Map<Long, Double> trainTime = null;
		
		Level level = Level.org;
		
		if(gradeId != null) {
			level = Level.grade;
		}
		if(trianIds != null) {
			try{
				trainTime = statisticsService.queryTrainTimeList(trianIds, level);
			}catch(Throwable e) {
				logError(e);
				return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
			}
		}
		
		Map<String, Object> result = new HashMap<>();
		
		result.put("trainSummary", array);
		
		result.put("trainTime", trainTime);
		
		result.put("studentSummary", studentSummary);
		
		return new ResultDTO<Map<String, Object>>(result);
	}

	private void logError(Throwable e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		pw.flush();
		log.error(sw.toString());
		
	}
	
	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/queryGradeJumpList4PC", method = RequestMethod.GET)
	public ResultDTO<?> queryGradeJumpList4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "开始时间", required = false) @RequestParam(name = "startTime", required = false) String beginTime,
			@ApiParam(value = "结束时间", required = false) @RequestParam(name = "endTime", required = false) String endTime)throws Exception{
		SimpleDateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", df.format(dfParse.parse(beginTime)));
		params.put("endTime", df.format(dfParse.parse(endTime)));
		params.put("projectKind", SportsProjectKindEnum.ROPESKIPPING.getValue());
		params.put("modelType", SportsModelTypeEnum.MOCKS.getValue());
		params.put("orgId", orgId);
		
		String trianIds;
		try{
			trianIds = statisticsService.getTrainIds(params);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}

		List<JumpGradeSummaryDto> summaryList = statisticsService.queryTrainGradeSummaryList(trianIds);
		return new ResultDTO<>(summaryList);
	}
	
	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/queryGradeRunList4PC", method = RequestMethod.GET)
	public ResultDTO<?> queryGradeRunList4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "开始时间", required = false) @RequestParam(name = "startTime", required = false) String beginTime,
			@ApiParam(value = "结束时间", required = false) @RequestParam(name = "endTime", required = false) String endTime)throws Exception{
		
		SimpleDateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", df.format(dfParse.parse(beginTime)));
		params.put("endTime", df.format(dfParse.parse(endTime)));
		params.put("projectKind", SportsProjectKindEnum.RUNNING.getValue());
		params.put("modelType", SportsModelTypeEnum.RUN1000.getValue());
		params.put("orgId", orgId);
		
		String trianIds;
		try{
			trianIds = statisticsService.getTrainIds(params);
		}catch(Throwable e) {
			logError(e);
			return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
		}

		List<JumpGradeSummaryDto> summaryList = statisticsService.queryTrainGradeSummaryList(trianIds);
		return new ResultDTO<>(summaryList);
	}

	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/queryClassJumpList4PC", method = RequestMethod.GET)
	public ResultDTO<?> queryClassJumpList4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId,
			@ApiParam(value = "开始时间", required = false) @RequestParam(name = "startTime", required = false) String beginTime,
			@ApiParam(value = "结束", required = false) @RequestParam(name = "endTime", required = false) String endTime)throws Exception{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", df.parse(beginTime));
		params.put("endTime", df.parse(endTime));
		params.put("orgId", orgId);
		params.put("gradeId", gradeId);
		params.put("projectKind", SportsProjectKindEnum.ROPESKIPPING.getValue());
		params.put("modelType", SportsModelTypeEnum.MOCKS.getValue());
		
		List<JumpClassSummaryDto> summaryList = statisticsService.queryTrainClassSummaryList(params);
		return new ResultDTO<>(summaryList);
	}

	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/queryClassRunList4PC", method = RequestMethod.GET)
	public ResultDTO<?> queryClassRunList4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId,
			@ApiParam(value = "开始时间", required = false) @RequestParam(name = "startTime", required = false) String beginTime,
			@ApiParam(value = "结束", required = false) @RequestParam(name = "endTime", required = false) String endTime)throws Exception{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", df.parse(beginTime));
		params.put("endTime", df.parse(endTime));
		params.put("orgId", orgId);
		params.put("gradeId", gradeId);
		params.put("projectKind", SportsProjectKindEnum.RUNNING.getValue());
		params.put("modelType", SportsModelTypeEnum.RUN1000.getValue());
		
		List<JumpClassSummaryDto> summaryList = statisticsService.queryTrainClassSummaryList(params);
		return new ResultDTO<>(summaryList);
	}

	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/queryJumpTrainList4PC", method = RequestMethod.GET)
	public ResultDTO<?> queryJumpTrainList4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "班级ID", required = false) @RequestParam(name = "classId", required = false) Long classId,
			@ApiParam(value = "开始时间", required = false) @RequestParam(name = "startTime", required = false) Date beginTime,
			@ApiParam(value = "结束", required = false) @RequestParam(name = "endTime", required = false) Date endTime,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize)throws Exception{
		Map<String, Object> params = new HashMap<>();
		params.put("beginTime", beginTime);
		params.put("endTime", endTime);
		params.put("orgId", orgId);
		params.put("classId", classId);
		params.put("projectKind", SportsProjectKindEnum.ROPESKIPPING.getValue());
		params.put("modelType", SportsModelTypeEnum.MOCKS.getValue());
		PageInfo<JumpTrainSummaryDto> summaryList = statisticsService.queryTrainSummaryList4PC(params, pageNo, pageSize);
		return new ResultDTO<>(summaryList);
	}

	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/getJumpTrain4PC", method = RequestMethod.GET)
	public ResultDTO<?> getJumpTrain4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "训练ID", required = false) @RequestParam(name = "classId", required = false) Long trainId)throws Exception{
		Map<String, Object> params = new HashMap<>();
		params.put("orgId", orgId);
		params.put("trainId", trainId);
		JumpTrainSummaryDto dto = statisticsService.getTrain(params);
		return new ResultDTO<>(dto);
	}

	@ApiOperation(value = "获取学校年级跳绳列表")
	@RequestMapping(value = "/getRunTrain4PC", method = RequestMethod.GET)
	public ResultDTO<?> getRunTrain4PC(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
			@ApiParam(value = "训练ID", required = false) @RequestParam(name = "classId", required = false) Long trainId)throws Exception{
		Map<String, Object> params = new HashMap<>();
		params.put("orgId", orgId);
		params.put("trainId", trainId);
		JumpTrainSummaryDto dto = statisticsService.getTrain(params);
		return new ResultDTO<>(dto);
	}
	
	
	@ApiOperation(value = "获取成绩趋势")
	@RequestMapping(value = "/getAvgScoreTrand", method = RequestMethod.GET)
	public ResultDTO<?> getAvgScoreTrand(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "项目类型", required = true) @RequestParam(name = "projectKind", required = true)  int projectKind,
			@ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
			@ApiParam(value = "老师ID", required = true) @RequestParam(name = "teacherId", required = true) Long teacherId,
			@ApiParam(value = "班级ID", required = false) @RequestParam(name = "classId", required = false) Long classId,
			@ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId)throws Exception{
		Map<String, Object> params = new HashMap<>();
		params.put("projectKind", projectKind);
		params.put("orgId", orgId);
		params.put("teacherId", teacherId);
		params.put("classId", classId);
		params.put("gradeId", gradeId);
		params.put("selectOne", true);
	    List<StatisticsAvgScoreDto> dto = statisticsService.getAvgScoreTrand(params);
		return new ResultDTO<>(dto);
	}

}
